<?php

/**
 * 菜单Model
 * @author Administrator
 *
 */
class System_menu_model extends MY_Model
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * 查找对应数据信息
     * @param unknown $type 菜单类型
     * @param int $parent_id 父类id
     * @return array
     */
    function get_system_menu_select($type, $parent_id)
    {
        if (!empty($type)) {
            if (!empty($parent_id)) {
                $array = array('type' => $type, 'parent_id' => $parent_id);//wehre 条件
            } else {
                $array = array('type' => $type);//wehre 条件
            }
            $this->db->where($array);
            $this->success['data'] = $this->db->select("system_menu.*")
                ->from("system_menu")
                ->get()
                ->result_array();
            return $this->success;//返回查询数据
        } else {
            return $this->param_error;//返回参数错误
        }
    }

    /**
     * 根据主键id查询单条数据
     * @param int $id 主键id
     * @return array
     */
    function get_system_menu_id($id)
    {
        if (!empty($id)) {
            $this->db->where('id', $id);//where
            $this->success['data'] = $this->db->get('system_menu')->row_array();
            return $this->success;//返回查询数据
        } else {
            return $this->param_error;//返回参数错误
        }
    }

    /**
     * 添加和编辑方法
     * @param array $data 添加和编辑数据 数组
     * @return array
     */
    function replace_system_menu($data)
    {
        if (!empty($data)) {
            if (isset($data['id']) && !empty($data['id'])) {//修改方法
                $this->db->where('id', $data['id']);
                $this->db->update("system_menu", $data);
                return $this->success;
            } else {//添加方法
                $data['create_time'] = time();
                $this->db->insert('system_menu', $data);
                return $this->success;
            }
        } else {
            return $this->param_error;
        }
    }
    /**
     * 查询全部菜单
     */
    function get_system_menu_all()
    {      
        $arr=array();//数据
        $this->db->where('s.type', '1');//一级类型where查询
        $main= $this->db->select("s.id,s.name,s.parent_id as pId")
        ->from("system_menu as s")->get()->result_array();//根据类型找出一级菜单
        if(!empty($main))
        {
            foreach ($main as $m){
                array_push($arr, $m);//赋值一类菜单
                $this->db->where('s1.parent_id', $m['id']);//一级菜单id查询
                $re=$this->db->select("s1.id,s1.name,s1.parent_id as pId")
                ->from("system_menu as s1")->get()->result_array();//查询属于一级菜单的二级菜单
                if(!empty($re)){
                    $arr=array_merge_recursive($arr, $re);
                }
            }
        }
        $this->success['data'] = $arr;
        return $this->success;
    }
}